package com.travel.haoma.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.TextView;

import com.travel.haoma.R;
import com.travel.haoma.common.Config;
import com.travel.haoma.view.MarkItemView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.haoma.base.common.ChoosePicActivity;
import cn.haoma.base.common.Id;
import cn.haoma.base.common.LayoutId;

/**
 * Created by admin on 2015/9/2.
 */
@LayoutId(R.layout.act_time_pieces)
public class ActTimePieces extends ChoosePicActivity {
    @Id(R.id.iv_content)
    private AbsoluteLayout ivContent;
    private static final int REQUEST_PIC = 100;
    private static final int REQUEST_TEXT = 101;
    private static final int REQUEST_EDIT = 102;
    @Id(R.id.type_content)
    private View addTypeContent;
    @Id(R.id.tv_feeling)
    private TextView tvFeeling;
    private List<MarkItemView> imgViews = new ArrayList<>();
    private List<MarkItemView> textViews = new ArrayList<>();
    public static Intent newIntent(Context context){
        return new Intent(context,ActTimePieces.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.title_back:
                finish();
                break;
            case R.id.bt_item1:
                addTypeContent.setVisibility(View.GONE);
                break;
            case R.id.bt_item2:
                if(addTypeContent.isShown()) addTypeContent.setVisibility(View.GONE);
                else addTypeContent.setVisibility(View.VISIBLE);
                break;
            case R.id.bt_item3:
                addTypeContent.setVisibility(View.GONE);
                break;
            case R.id.bt_add_img:
                addTypeContent.setVisibility(View.GONE);
                if(imgViews.size()>=4) {
                    showToast("最多添加四个图片哦");
                    break;
                }
                File derectory = new File(Config.CACHE_IMG_PATH);
                derectory.mkdirs();
                File file = new File(Config.CACHE_IMG_PATH,"img"+System.currentTimeMillis()+".png");
                try {
                    if(!file.exists())
                        file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                showPicDialog(file,REQUEST_PIC);
                break;
            case R.id.bt_add_text:
                addTypeContent.setVisibility(View.GONE);
                startActivityForResult(ActAddFeel.newIntent(this),REQUEST_TEXT);
                break;
        }
    }
    private void init(){
        ivContent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                for (MarkItemView view : imgViews) {
                    view.setEditState(false);
                }
                for (MarkItemView view : textViews) {
                    view.setEditState(false);
                }
                return false;
            }
        });
    }
    @Override
    protected void onPicSelected(File file, int reqId) {
        if(reqId==REQUEST_PIC){
            addImgMarkView(file);
        }

    }
    @Override
    protected void onCropPicSelected(File cropFile, int reqId) {

    }


    /**
     * 添加图片水印
     */
    private void addImgMarkView(File file)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap markBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),options);
        int width = options.outWidth;
        int height = options.outHeight;
        int maxWidth = getScreenPoint().x/3;
        int maxHeight = getScreenPoint().y/3;
        float scaleX = width*1.0f/maxWidth;
        float scaleY = height*1.0f/maxHeight;
        float scale = Math.max(scaleX,scaleY);
        options.inJustDecodeBounds = false;
        options.inSampleSize = (int) scale;
        markBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),options);
        addMarker(markBitmap,1,file.getAbsolutePath());
    }

    /**
     * 添加水印
     * @param markBitmap
     * @param type 1 图片  2文字
     * @param string  图片路径 或 文字
     */
    private void addMarker(Bitmap markBitmap, final int type, final String string){
        final MarkItemView markView = new MarkItemView( this);
        markView.setScreen_H(getScreenPoint().y);
        markView.setScreen_W(getScreenPoint().x);
        markView.setTag(type);
        AbsoluteLayout.LayoutParams   decorationParams = new AbsoluteLayout.LayoutParams(markBitmap.getWidth(),
                markBitmap.getHeight(), getScreenPoint().x / 4,
                getScreenPoint().y / 3);

        markView.setImageBitmap(markBitmap,
                new Rect(getScreenPoint().x / 4,getScreenPoint().y / 3,
                        getScreenPoint().x / 4 + markBitmap.getWidth(),
                        getScreenPoint().y / 3 + markBitmap.getHeight()));
        ivContent.addView(markView, decorationParams);
        for (MarkItemView view : imgViews){
            if(view!=markView)
                view.setEditState(false);
        }
        for (MarkItemView view : textViews){
            if(view!=markView)
                view.setEditState(false);
        }
        if(type==1) imgViews.add(markView);
        if(type==2) textViews.add(markView);
        markView.setCallbackListener(new MarkItemView.OnCallbackListener() {
            @Override
            public void onTouchThis() {
                for (MarkItemView view : imgViews){
                    if(view!=markView)
                        view.setEditState(false);
                }
                for (MarkItemView view : textViews){
                    if(view!=markView)
                        view.setEditState(false);
                }
            }

            @Override
            public void onDeleted() {
                if(type==1) imgViews.remove(markView);
                if(type==2) textViews.remove(markView);
            }

            @Override
            public void onEditClick() {
                startActivityForResult(ActEditPic.newIntent(ActTimePieces.this,string),REQUEST_EDIT);
            }
        });
    }

    /**
     * 添加文字水印
     * @param string
     */
    private void addTextMarkerView(String string){
        tvFeeling.setText(string);
        tvFeeling.setDrawingCacheEnabled(true);
        Bitmap bitmap = tvFeeling.getDrawingCache();
        bitmap = Bitmap.createBitmap(bitmap);
        tvFeeling.setDrawingCacheEnabled(false);
        addMarker(bitmap, 2,string);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK&&requestCode==REQUEST_TEXT){
            addTextMarkerView(ActAddFeel.processActivityResult(data));

        }
    }
}
